app.controller('pv1', function ($scope, $rootScope, $http, $interval, $compile, $state) {

    //pv任务
    $scope.pv3_0 = function (id) {

        localStorage.removeItem("pv-feSignUrl");
        localStorage.removeItem("pv-customerSignUrl");

        pv3_0.params.id = id;
        localStorage.setItem("pv-caseId",id);
        $state.go(pv3_0.stateName);
    }
    //pv任务选择原因
    $scope.pv6 = function (id) {
        pv6.params.id = id;
        localStorage.setItem("pv-caseId",id);
        $state.go(pv6.stateName);
    }

    //获得数据
    $scope.getOrders = function (status,contactFlag) {
        if("所有"==status){
            $scope.status = "";
        }else if(status){
            $scope.status = status;
        }
        var customerName = document.getElementById("input-customeName");
        if(customerName){
            $scope.customeName = customerName.value;
        }
//        else{
//        	$scope.customeName = sessionStorage.getItem("username");
//        }

        // $scope.status = status; //为下拉刷新做准备
        var postData = {
            currPage : $scope.currentPage,
            pageSize : $scope.pageSize,
            status : $scope.status,
            customeName:$scope.customeName
        };
        postHttp($http, 'pvCase/list', postData, function (res) {
            $scope.customeFlag = false;
            if (res.code === 0 && res.msg == "success") {
                $scope.totalCount = res.data.totalCount; //客户总数
                $scope.caseTotal = res.data.caseTotal;  //案例总数
                $scope.total = res.data.total;  //设备数量
                $scope.newCaseTotal = res.data.newCaseTotal; //新派单数


                // $scope.totalPage = res.data.totalPage;
                if ($scope.refreshFlag) {
                    $scope.pageData = [];
                    $scope.refreshFlag = false;
                }
                $scope.pageCount = res.data.totalPage;
                if(true == contactFlag){
                    $scope.pageData = $scope.pageData.concat(res.data.list || []);
                }else{
                    $scope.pageData = res.data.list;
                }

                //设置选中标记

                for(var i=0;i<$scope.pageData.length;i++){
                    var ypd = 0;var yzt = 0; var dqz=0;
                    var obj = $scope.pageData[i];
                    var pvcase = obj.pvCases;
                    if(!pvcase){
                        continue;
                    }
                    for(var k=0;k<pvcase.length;k++){
                        var objk = pvcase[k];
                        //统计已派单，已暂停，待签字个数
                        if(objk.status == 2){
                            ypd+=1;
                        }else if(objk.status == 3){
                            yzt+=1;
                        }else if(objk.status == 4){
                            dqz+=1;
                        }
                    }
                    obj.summaryMessage = "已派单:"+ypd+"    已暂停:"+yzt+"     待签字:"+dqz;
                }

                $scope.$applyAsync();
            } else {
                $rootScope.tipPopups.show(($rootScope.tipMessage = res.message));
            }
        });
    }

    $scope.checkCustomeName = function () {
        $scope.customeFlag = true;
        $scope.$applyAsync();
    }

    $scope.transfer = function (json,value) {
        var obj = JSON.parse(json);
        return obj[0][value];
    }

    //跳转页面
    $scope.jumpTo = function(type,id){
        if(1==type){
            $scope.pv3_0(id);
        }else if(2==type){
            localStorage.setItem("pv-isfond",0);
            $scope.pv6(id);
        }else{
            mine.params.type = "pv";
            $state.go(mine.stateName);
        }
    }


    //签字
    $scope.sign = function (item) {
        var signCaseIdAry = [];
        for(var i=0;i<item.length;i++){
            var obj = item[i];

            if(obj.status == 4){
                signCaseIdAry.push(obj.caseId);
            }
        }
        if(signCaseIdAry.length==0){
            $rootScope.tipPopups.show(($rootScope.tipMessage = "暂没有待签字的单子"));
            return;
        }

        localStorage.setItem("pvSignCaseIdAry",JSON.stringify(signCaseIdAry));
        $state.go(pvconfirmsign.stateName);
    }

    var options = {
        container: '#minirefresh',
        down: {
            isLock:true,
            callback: function () {
                // 下拉事件
                $scope.getOrders("");
                miniRefresh.endDownLoading();
            }
        },
        up: {
            isLock: false,
            isAuto: false,
            loadFull: {
                isEnable: false
            },
            offset: 200,
            isShowUpLoading: false,
            callback: function () {
                // 上拉事件
                $scope.nextPage();
                // 注意，由于默认情况是开启满屏自动加载的，所以请求失败时，请务必endUpLoading(true)，防止无限请求
                miniRefresh.endUpLoading(true, 'success');
            }
        }
    }
    var miniRefresh;
    setTimeout(function () {
        miniRefresh = new MiniRefresh(options);
    }, 1000);
    $scope.$watch('allData', function (newV, oldV) {
        if (newV != oldV)
            if (miniRefresh)
                miniRefresh.resetUpLoading();
    }, true)


    $scope.nextPage = function () {
        if ($scope.pageCount > $scope.currentPage) {
            $scope.currentPage++;
            $scope.getOrders("",true);
        }
    }

    //初始化
    function init() {
		
		if(checkPhone("iphone")){
			try{
				window.webkit.messageHandlers.requestGPS.postMessage("");
			}catch(e){
				 $rootScope.tipPopups.show(($rootScope.tipMessage = JSON.stringify(e)));
			}
		 }
		
        $scope.refreshFlag = true;
        $scope.currentPage = 1;
        $scope.pageSize = 10;
        $scope.pageCount = 1;
        $scope.customeFlag = true;
        localStorage.removeItem("pvSignCaseIdAry");
        localStorage.removeItem("pv-countfeSignUrl");
        localStorage.removeItem("pv-countcustomerSignUrl");
        localStorage.removeItem("pv-isfond");
        localStorage.removeItem("pv-caseId");

        $scope.getOrders("");
        $scope.$applyAsync();
    }

    init();


   reloadJs();
});
function getPvOrders(param) {
    var $scope = getScope("pv1");
    $scope.getOrders(param,false);
}

//检查输入框
function checkPvCustomeName() {
    var $scope = getScope("pv1");
    $scope.checkCustomeName();
}